home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jslock.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-08  |  10.5 KB  |  262 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39. #ifndef jslock_h__
  40. #define jslock_h__
  41.  
  42. #ifdef JS_THREADSAFE
  43.  
  44. #include "jstypes.h"
  45. #include "pratom.h"
  46. #include "prlock.h"
  47. #include "prcvar.h"
  48.  
  49. #include "jsprvtd.h"    /* for JSScope, etc. */
  50. #include "jspubtd.h"    /* for JSRuntime, etc. */
  51.  
  52. #define Thin_GetWait(W) ((jsword)(W) & 0x1)
  53. #define Thin_SetWait(W) ((jsword)(W) | 0x1)
  54. #define Thin_RemoveWait(W) ((jsword)(W) & ~0x1)
  55.  
  56. typedef struct JSFatLock JSFatLock;
  57.  
  58. struct JSFatLock {
  59.     int         susp;
  60.     PRLock      *slock;
  61.     PRCondVar   *svar;
  62.     JSFatLock   *next;
  63.     JSFatLock   **prevp;
  64. };
  65.  
  66. typedef struct JSThinLock {
  67.     jsword      owner;
  68.     JSFatLock   *fat;
  69. } JSThinLock;
  70.  
  71. typedef PRLock JSLock;
  72.  
  73. typedef struct JSFatLockTable {
  74.     JSFatLock   *free;
  75.     JSFatLock   *taken;
  76. } JSFatLockTable;
  77.  
  78. /*
  79.  * Atomic increment and decrement for a reference counter, given jsrefcount *p.
  80.  * NB: jsrefcount is int32, aka PRInt32, so that pratom.h functions work.
  81.  */
  82. #define JS_ATOMIC_INCREMENT(p)      PR_AtomicIncrement((PRInt32 *)(p))
  83. #define JS_ATOMIC_DECREMENT(p)      PR_AtomicDecrement((PRInt32 *)(p))
  84. #define JS_ATOMIC_ADD(p,v)          PR_AtomicAdd((PRInt32 *)(p), (PRInt32)(v))
  85.  
  86. #define CurrentThreadId()           (jsword)PR_GetCurrentThread()
  87. #define JS_CurrentThreadId()        js_CurrentThreadId()
  88. #define JS_NEW_LOCK()               PR_NewLock()
  89. #define JS_DESTROY_LOCK(l)          PR_DestroyLock(l)
  90. #define JS_ACQUIRE_LOCK(l)          PR_Lock(l)
  91. #define JS_RELEASE_LOCK(l)          PR_Unlock(l)
  92. #define JS_LOCK0(P,M)               js_Lock(P,M)
  93. #define JS_UNLOCK0(P,M)             js_Unlock(P,M)
  94.  
  95. #define JS_NEW_CONDVAR(l)           PR_NewCondVar(l)
  96. #define JS_DESTROY_CONDVAR(cv)      PR_DestroyCondVar(cv)
  97. #define JS_WAIT_CONDVAR(cv,to)      PR_WaitCondVar(cv,to)
  98. #define JS_NO_TIMEOUT               PR_INTERVAL_NO_TIMEOUT
  99. #define JS_NOTIFY_CONDVAR(cv)       PR_NotifyCondVar(cv)
  100. #define JS_NOTIFY_ALL_CONDVAR(cv)   PR_NotifyAllCondVar(cv)
  101.  
  102. /*
  103.  * Include jsscope.h so JS_LOCK_OBJ macro callers don't have to include it.
  104.  * Since there is a JSThinLock member in JSScope, we can't nest this include
  105.  * much earlier (see JSThinLock's typedef, above).  Yes, that means there is
  106.  * an #include cycle between jslock.h and jsscope.h: moderate-sized XXX here,
  107.  * to be fixed by moving JS_LOCK_SCOPE to jsscope.h, JS_LOCK_OBJ to jsobj.h,
  108.  * and so on.
  109.  */
  110. #include "jsscope.h"
  111.  
  112. #define JS_LOCK_RUNTIME(rt)         js_LockRuntime(rt)
  113. #define JS_UNLOCK_RUNTIME(rt)       js_UnlockRuntime(rt)
  114.  
  115. /*
  116.  * NB: The JS_LOCK_OBJ and JS_UNLOCK_OBJ macros work *only* on native objects
  117.  * (objects for which OBJ_IS_NATIVE returns true).  All uses of these macros in
  118.  * the engine are predicated on OBJ_IS_NATIVE or equivalent checks.  These uses
  119.  * are for optimizations above the JSObjectOps layer, under which object locks
  120.  * normally hide.
  121.  */
  122. #define JS_LOCK_OBJ(cx,obj)         ((OBJ_SCOPE(obj)->ownercx == (cx))        \
  123.                                      ? (void)0                                \
  124.                                      : (js_LockObj(cx, obj)))
  125. #define JS_UNLOCK_OBJ(cx,obj)       ((OBJ_SCOPE(obj)->ownercx == (cx))        \
  126.                                      ? (void)0 : js_UnlockObj(cx, obj))
  127.  
  128. #define JS_LOCK_SCOPE(cx,scope)     ((scope)->ownercx == (cx) ? (void)0       \
  129.                                      : js_LockScope(cx, scope))
  130. #define JS_UNLOCK_SCOPE(cx,scope)   ((scope)->ownercx == (cx) ? (void)0       \
  131.                                      : js_UnlockScope(cx, scope))
  132. #define JS_TRANSFER_SCOPE_LOCK(cx, scope, newscope)                           \
  133.                                     js_TransferScopeLock(cx, scope, newscope)
  134.  
  135. extern jsword js_CurrentThreadId();
  136. extern void js_LockRuntime(JSRuntime *rt);
  137. extern void js_UnlockRuntime(JSRuntime *rt);
  138. extern void js_LockObj(JSContext *cx, JSObject *obj);
  139. extern void js_UnlockObj(JSContext *cx, JSObject *obj);
  140. extern void js_LockScope(JSContext *cx, JSScope *scope);
  141. extern void js_UnlockScope(JSContext *cx, JSScope *scope);
  142. extern int js_SetupLocks(int,int);
  143. extern void js_CleanupLocks();
  144. extern void js_InitContextForLocking(JSContext *);
  145. extern void js_TransferScopeLock(JSContext *, JSScope *, JSScope *);
  146. extern JS_FRIEND_API(jsval)
  147. js_GetSlotThreadSafe(JSContext *, JSObject *, uint32);
  148. extern void js_SetSlotThreadSafe(JSContext *, JSObject *, uint32, jsval);
  149. extern void js_InitLock(JSThinLock *);
  150. extern void js_FinishLock(JSThinLock *);
  151. extern void js_FinishSharingScope(JSRuntime *rt, JSScope *scope);
  152.  
  153. #ifdef DEBUG
  154.  
  155. #define JS_IS_RUNTIME_LOCKED(rt)        js_IsRuntimeLocked(rt)
  156. #define JS_IS_OBJ_LOCKED(cx,obj)        js_IsObjLocked(cx,obj)
  157. #define JS_IS_SCOPE_LOCKED(cx,scope)    js_IsScopeLocked(cx,scope)
  158.  
  159. extern JSBool js_IsRuntimeLocked(JSRuntime *rt);
  160. extern JSBool js_IsObjLocked(JSContext *cx, JSObject *obj);
  161. extern JSBool js_IsScopeLocked(JSContext *cx, JSScope *scope);
  162.  
  163. #else
  164.  
  165. #define JS_IS_RUNTIME_LOCKED(rt)        0
  166. #define JS_IS_OBJ_LOCKED(cx,obj)        1
  167. #define JS_IS_SCOPE_LOCKED(cx,scope)    1
  168.  
  169. #endif /* DEBUG */
  170.  
  171. #define JS_LOCK_OBJ_VOID(cx, obj, e)                                          \
  172.     JS_BEGIN_MACRO                                                            \
  173.         JS_LOCK_OBJ(cx, obj);                                                 \
  174.         e;                                                                    \
  175.         JS_UNLOCK_OBJ(cx, obj);                                               \
  176.     JS_END_MACRO
  177.  
  178. #define JS_LOCK_VOID(cx, e)                                                   \
  179.     JS_BEGIN_MACRO                                                            \
  180.         JSRuntime *_rt = (cx)->runtime;                                       \
  181.         JS_LOCK_RUNTIME_VOID(_rt, e);                                         \
  182.     JS_END_MACRO
  183.  
  184. #if defined(JS_USE_ONLY_NSPR_LOCKS) ||                                        \
  185.     !( (defined(_WIN32) && defined(_M_IX86)) ||                               \
  186.        (defined(__GNUC__) && defined(__i386__)) ||                            \
  187.        (defined(SOLARIS) && defined(sparc) && defined(ULTRA_SPARC)) ||        \
  188.        defined(AIX) )
  189.  
  190. #define NSPR_LOCK 1
  191.  
  192. #undef JS_LOCK0
  193. #undef JS_UNLOCK0
  194. #define JS_LOCK0(P,M)   (JS_ACQUIRE_LOCK(((JSLock*)(P)->fat)), (P)->owner = (M))
  195. #define JS_UNLOCK0(P,M) ((P)->owner = 0, JS_RELEASE_LOCK(((JSLock*)(P)->fat)))
  196.  
  197. #else  /* arch-tests */
  198.  
  199. #undef NSPR_LOCK
  200.  
  201. extern JS_INLINE void js_Lock(JSThinLock *tl, jsword me);
  202. extern JS_INLINE void js_Unlock(JSThinLock *tl, jsword me);
  203.  
  204. #endif /* arch-tests */
  205.  
  206. #else  /* !JS_THREADSAFE */
  207.  
  208. #define JS_ATOMIC_INCREMENT(p)      (++*(p))
  209. #define JS_ATOMIC_DECREMENT(p)      (--*(p))
  210. #define JS_ATOMIC_ADD(p,v)          (*(p) += (v))
  211.  
  212. #define JS_CurrentThreadId() 0
  213. #define JS_NEW_LOCK()               NULL
  214. #define JS_DESTROY_LOCK(l)          ((void)0)
  215. #define JS_ACQUIRE_LOCK(l)          ((void)0)
  216. #define JS_RELEASE_LOCK(l)          ((void)0)
  217. #define JS_LOCK0(P,M)               ((void)0)
  218. #define JS_UNLOCK0(P,M)             ((void)0)
  219.  
  220. #define JS_NEW_CONDVAR(l)           NULL
  221. #define JS_DESTROY_CONDVAR(cv)      ((void)0)
  222. #define JS_WAIT_CONDVAR(cv,to)      ((void)0)
  223. #define JS_NOTIFY_CONDVAR(cv)       ((void)0)
  224. #define JS_NOTIFY_ALL_CONDVAR(cv)   ((void)0)
  225.  
  226. #define JS_LOCK_RUNTIME(rt)         ((void)0)
  227. #define JS_UNLOCK_RUNTIME(rt)       ((void)0)
  228. #define JS_LOCK_OBJ(cx,obj)         ((void)0)
  229. #define JS_UNLOCK_OBJ(cx,obj)       ((void)0)
  230. #define JS_LOCK_OBJ_VOID(cx,obj,e)  (e)
  231. #define JS_LOCK_SCOPE(cx,scope)     ((void)0)
  232. #define JS_UNLOCK_SCOPE(cx,scope)   ((void)0)
  233. #define JS_TRANSFER_SCOPE_LOCK(c,o,n) ((void)0)
  234.  
  235. #define JS_IS_RUNTIME_LOCKED(rt)        1
  236. #define JS_IS_OBJ_LOCKED(cx,obj)        1
  237. #define JS_IS_SCOPE_LOCKED(cx,scope)    1
  238. #define JS_LOCK_VOID(cx, e)             JS_LOCK_RUNTIME_VOID((cx)->runtime, e)
  239.  
  240. #endif /* !JS_THREADSAFE */
  241.  
  242. #define JS_LOCK_RUNTIME_VOID(rt,e)                                            \
  243.     JS_BEGIN_MACRO                                                            \
  244.         JS_LOCK_RUNTIME(rt);                                                  \
  245.         e;                                                                    \
  246.         JS_UNLOCK_RUNTIME(rt);                                                \
  247.     JS_END_MACRO
  248.  
  249. #define JS_LOCK_GC(rt)              JS_ACQUIRE_LOCK((rt)->gcLock)
  250. #define JS_UNLOCK_GC(rt)            JS_RELEASE_LOCK((rt)->gcLock)
  251. #define JS_LOCK_GC_VOID(rt,e)       (JS_LOCK_GC(rt), (e), JS_UNLOCK_GC(rt))
  252. #define JS_AWAIT_GC_DONE(rt)        JS_WAIT_CONDVAR((rt)->gcDone, JS_NO_TIMEOUT)
  253. #define JS_NOTIFY_GC_DONE(rt)       JS_NOTIFY_ALL_CONDVAR((rt)->gcDone)
  254. #define JS_AWAIT_REQUEST_DONE(rt)   JS_WAIT_CONDVAR((rt)->requestDone,        \
  255.                                                     JS_NO_TIMEOUT)
  256. #define JS_NOTIFY_REQUEST_DONE(rt)  JS_NOTIFY_CONDVAR((rt)->requestDone)
  257.  
  258. #define JS_LOCK(P,CX)               JS_LOCK0(P,(CX)->thread)
  259. #define JS_UNLOCK(P,CX)             JS_UNLOCK0(P,(CX)->thread)
  260.  
  261. #endif /* jslock_h___ */
  262.